package com.ravi.controller;

import com.ravi.common.R;
import com.ravi.entity.AdminUser;
import com.ravi.request.AdminUserForm;
import com.ravi.service.AdminUserService;
import com.ravi.util.jwt.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("user")
@Slf4j
public class AdminUserController {

	@Autowired
	private AdminUserService userService;

	@PostMapping("login")
	public R login(@RequestBody AdminUserForm loginForm) {
		AdminUser user = new AdminUser();
		user.setWorkId(loginForm.getWorkId());
		user.setPassword(loginForm.getPassword());
		log.info("当前登录用户的信息: [{}]",user);
		try{
			AdminUser userDB = userService.login(user);
			Map<String,String> paras = new HashMap<>();
			paras.put("workId",userDB.getWorkId());
			paras.put("areaId", String.valueOf(userDB.getAreaId()));
			String token = JWTUtils.getAccessToken(paras);
			return R.ok().put("user", userDB).put("token", token);
		}catch (Exception e) {
			return R.error(e.getMessage());
		}
	}
}
